package com.foreknow.service.impl;

import com.foreknow.dao.ConstantitemMapper;
import com.foreknow.dao.DepartmentMapper;
import com.foreknow.dao.RegistlevelMapper;
import com.foreknow.dao.UserMapper;
import com.foreknow.entity.Constantitem;
import com.foreknow.entity.Department;
import com.foreknow.entity.Registlevel;
import com.foreknow.entity.User;
import com.foreknow.service.UserService;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ConstantitemMapper constantitemMapper;
    @Autowired
    private DepartmentMapper departmentMapper;
    @Autowired
    private RegistlevelMapper registlevelMapper;
    @Override
    public int deleteByPrimaryKey(Integer id) {

        return 0;
    }

    @Override
    public int deleteByPrimaryKeyByLogic(Integer id)  {
        try {
            int i = userMapper.deleteByPrimaryKeyByLogic(id);
            return i;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int deleteByIdByLogicBatch(List<Integer> ids) {
        try {
            return userMapper.deleteByIdByLogicBatch(ids);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int insert(User record) {
        try {
            int insert = userMapper.insert(record);
            return insert;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public Map<String,Object> excelToList(InputStream inputStream) throws IOException, InvalidFormatException {
        //装载插入对象
        List<User> gisList=new ArrayList<User>();
        //装载错误信息
        List<String> errorList=new ArrayList<String>();
        //装载用户类型
        HashMap<String, Integer> usetypeMap = new HashMap<>();
        usetypeMap.put("医院管理员",1);
        usetypeMap.put("挂号收费员",2);
        usetypeMap.put("门诊医生",3);
        usetypeMap.put("医技医生",4);
        usetypeMap.put("药房操作员",5);
        usetypeMap.put("财务管理员",6);

        Workbook workbook = null;
        workbook = WorkbookFactory.create(inputStream);
        inputStream.close();
        //工作表对象
        Sheet sheet = workbook.getSheetAt(0);
        //总行数
        int rowLength = sheet.getLastRowNum() + 1;
        //工作表的列
        Row row = sheet.getRow(0);
        //总列数
        int colLength = row.getLastCellNum();
        for (int i = 1; i < rowLength; i++) {
            if (sheet.getRow(i)==null){continue;}
            row = sheet.getRow(i);

            if (row.getCell(0)==null || "".equals(row.getCell(0))){
                if (i==1){
                    errorList.add("请按规范添加相关数据项");
                }
                break;
            }
            row.getCell(0).setCellType(CellType.STRING);
            String cell0 = row.getCell(0).getStringCellValue().trim();
            String cell1="";
            String cell2="";
            String cell3="";
            String cell4="";
            String cell5="";
            String cell6="";
            String cell7="";
            String cell8="";

            if (row.getCell(1)!=null){
                row.getCell(1).setCellType(CellType.STRING);
                cell1 = row.getCell(1).getStringCellValue().trim();
            }
            if (row.getCell(2)!=null){
                row.getCell(2).setCellType(CellType.STRING);
                cell2 = row.getCell(2).getStringCellValue().trim();
            }
            if (row.getCell(3)!=null){
                row.getCell(3).setCellType(CellType.STRING);
                cell3 = row.getCell(3).getStringCellValue().trim();
            }
            if (row.getCell(4)!=null){
                row.getCell(4).setCellType(CellType.STRING);
                cell4 = row.getCell(4).getStringCellValue().trim();
            }
            if (row.getCell(5)!=null){
                row.getCell(5).setCellType(CellType.STRING);
                cell5 = row.getCell(5).getStringCellValue().trim();
            }
            if (row.getCell(6)!=null){
                row.getCell(6).setCellType(CellType.STRING);
                cell6 = row.getCell(6).getStringCellValue().trim();
            }
            if (row.getCell(7)!=null){
                row.getCell(7).setCellType(CellType.STRING);
                cell7 = row.getCell(7).getStringCellValue().trim();
            }
            if (row.getCell(8)!=null){
                row.getCell(8).setCellType(CellType.STRING);
                cell8 = row.getCell(8).getStringCellValue().trim();
            }


            User user=new User();
            //验证用户名
            try {
                if ("".equals(cell1)){
                    errorList.add("在序列第"+cell0+"号"+"用户名项不能为空");
                }else{
                    User test = userMapper.selectByUserName(cell1);
                    if (test!=null){
                        errorList.add("在序列第"+cell0+"号"+"用户名 "+cell1+" 已存在");
                    }
                    else
                        user.setUsername(cell1);
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
            //验证密码
            if ("".equals(cell2)){
                errorList.add("在序列第"+cell0+"号"+"密码项不能为空");
            }else
                user.setPassword(cell2);
            //验证真实姓名
            if ("".equals(cell3)){
                errorList.add("在序列第"+cell0+"号"+"真实姓名项不能为空");
            }else
                user.setRealname(cell3);

            //验证用户类型
            Integer cell4type= 0;
            if ("".equals(cell4)){
                errorList.add("在序列第"+cell0+"号"+"用户类型项不能为空");
            }
            else if(!"医院管理员".equals(cell4) && !"挂号收费员".equals(cell4) && !"门诊医生".equals(cell4)
                    && !"医技医生".equals(cell4) && !"药房操作员".equals(cell4) && !"财务管理员".equals(cell4)){
                errorList.add("在序列第"+cell0+"号"+"用户类型项不正确");
            }else{
                cell4type = usetypeMap.get(cell4);
                user.setUsetype(cell4type);
            }
            //验证医生职称
            Integer cell5id=-1;
            if ("".equals(cell5) && cell4type==3){
                errorList.add("在序列第"+cell0+"号"+"用户类型为门诊医生时医生职称项不能为空");
            }else if ("".equals(cell5)){
                user.setDoctitleid(null);
            }else{
                List<Constantitem> list = constantitemMapper.selectByConstantTypeID(8);
                for (Constantitem item : list) {
                    if (item.getConstantname().equals(cell5)){
                        cell5id=item.getId();
                        break;
                    }
                }
                if (cell5id==-1 &&cell4type==3){
                    errorList.add("在序列第"+cell0+"号"+"没有 "+cell5+" 此医生职称项");
                }else  user.setDoctitleid(null);
            }

            //验证是否排班
            if ("".equals(cell6)){
                cell6="否";
                user.setIsscheduling(cell6);
            }else if (!"是".equals(cell6) && !"否".equals(cell6)){
                errorList.add("在序列第"+cell0+"号"+"是否参与排班项只能填写‘是’或‘否’，不填写默认为‘否’");
            }else  user.setIsscheduling(cell6);

            //验证科室情况
            if ("".equals(cell7)){
                errorList.add("在序列第"+cell0+"号"+"科室项不能为空");
            }else {
                int index=2;
                if (cell4type==3) index=1;
                //查询所有科室类型
                for(int j=index; j < 6 ; j++) {
                    List<Department> departments = departmentMapper.selectAllDepartmentByCondition(j);
                    if (index!=1 && j==1){continue;}
                    for (Department item : departments) {
                        if (item.getDeptname().equals(cell7)){
                            user.setDeptid(item.getId());
                            break;
                        }
                    }
                    if (index==1){break;}
                }
                if (user.getDeptid()!=null && index==3){
                    errorList.add("在序列第"+cell0+"号"+"用户类型为门诊医生时 "+cell7+" 该科室项不在临床部门或不存在");
                }
                else  if (user.getDeptid()==null){
                    errorList.add("在序列第"+cell0+"号 "+cell7+" 科室项不存在");
                }

            }

            //验证挂号等级
            if ("".equals(cell8) && cell4type==3){
                errorList.add("在序列第"+cell0+"号"+"用户类型为门诊医生时医生挂号等级项不能为空");
            }else if (cell4type==3){
                //查询挂号等级
                List<Registlevel> registlevels = registlevelMapper.selectAll();
                for (Registlevel item : registlevels) {
                    if (item.getRegistname().equals(cell8)){
                        user.setRegistleid(item.getId());
                        break;
                    }
                }
                if (user.getRegistleid()==null){
                    errorList.add("在序列第"+cell0+"号"+"用户类型为门诊医生时医生挂号等级项 "+cell8+" 不存在");
                }

            }else user.setRegistleid(null);

            gisList.add(user);
        }
        //是否成功标志
        int flag=0;
        HashMap<String, Object> returnMap = new HashMap<>();
        //失败
        if (errorList.size()>0){
            returnMap.put("errorList",errorList);
            returnMap.put("flag",flag);
            return returnMap;
        }
        //成功
        flag = userMapper.saveUserBatch(gisList);
        returnMap.put("flag",flag);
        return returnMap ;
    }

    @Override
    public User login(User loginUser) {
        try {
            return userMapper.selectByUserNamePassword(loginUser);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public User selectByPrimaryKey(Integer id) {
        try {
            User user = userMapper.selectByPrimaryKey(id);
            return user;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public User selectByUserName(String userName) {
        try {
            User user = userMapper.selectByUserName(userName);
            return user;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<User> selectByCondition(User record) {
        try {
            return userMapper.selectByCondition(record);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int updateByPrimaryKeySelective(User record) {
        try {
            int i = userMapper.updateByPrimaryKeySelective(record);
            return i;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int updateByPrimaryKey(User record) {
        return 0;
    }


}
